﻿// FormAddin.cpp : Implementation of CFormAddin

#include "stdafx.h"
#include "FormAddin.h"
#include "SettingForm.h"


_ATL_FUNC_INFO OnClickSettingButtonInfo = {CC_STDCALL, VT_EMPTY, 2, {VT_DISPATCH,VT_BYREF | VT_BOOL }};
_ATL_FUNC_INFO OnClickSendButtonInfo = {CC_STDCALL, VT_EMPTY, 2, {VT_DISPATCH,VT_BYREF | VT_BOOL }};
// CFormAddin
STDMETHODIMP CFormAddin::OnConnection(LPDISPATCH Application, ext_ConnectMode ConnectMode, LPDISPATCH AddInInst, SAFEARRAY * * custom)
{
	spAppCache = Application;//get the outlook application iteself
	ATLASSERT(spAppCache);
	//---------------------------
	//Outlook 10.0.0.6515, 1033 (XP SP3) not totally support Unicode
	//Outlook 11.0.0.8169, 1033 (2003 SP3)
	//Outlook 12.0.0.4518, 1033 (2007)
	//Outlook 12.0.0.6423, 1033 (2007 SP2)
	//Outlook 14.0.0.4760, 1033 (2010)
	//---------------------------
	if(wcscmp(L"Outlook", spAppCache->Name.Detach()) != 0) return S_OK;
	if(spAppCache->Version.Detach()[1] > '1') return S_OK;

	auto spExplorer = spAppCache->ActiveExplorer();
	ATLASSERT(spExplorer);
	auto spCmdBars = spExplorer->CommandBars;
	ATLASSERT(spCmdBars);

	CComVariant vName(L"Wicresoft OMS Client 2003");
	CComVariant vPos(1);
	CComVariant vTemp(VARIANT_TRUE);
	CComVariant vEmpty(DISP_E_PARAMNOTFOUND , VT_ERROR);

	auto spNewBar = spCmdBars->Add(vName,vPos,vEmpty,vTemp);
	ATLASSERT(spNewBar);
	auto spNewBarCtrls = spNewBar->Controls;
	ATLASSERT(spNewBarCtrls);
	auto spNewCtrl1 = spNewBarCtrls->Add();
	ATLASSERT(spNewCtrl1);
	auto spNewCtrl2 = spNewBarCtrls->Add();
	ATLASSERT(spNewCtrl2);

	spButtonSetting = spNewCtrl1;
	spButtonSend = spNewCtrl2;


	//spNewCtrl->Caption = L"SMS Setting";
	//spNewCtrl->Enabled = true;
	//spNewCtrl->Visible = true;
	spButtonSetting->Caption = L"SMS Setting";
	spButtonSetting->Enabled = true;
	spButtonSetting->Visible = true;
	spButtonSend->Caption = L"SMS Send";
	spButtonSend->Enabled = true;
	spButtonSend->Visible = true;

	spNewBar->Visible = true;

	IDispEventSimpleImpl<1,CFormAddin,&__uuidof(Office::_CommandBarButtonEvents)>::DispEventAdvise((IDispatch*)spButtonSetting);
	IDispEventSimpleImpl<2,CFormAddin,&__uuidof(Office::_CommandBarButtonEvents)>::DispEventAdvise((IDispatch*)spButtonSend);

	return S_OK;
}

STDMETHODIMP CFormAddin::OnDisconnection(ext_DisconnectMode RemoveMode, SAFEARRAY * * custom)
{
	IDispEventSimpleImpl<1,CFormAddin,&__uuidof(Office::_CommandBarButtonEvents)>::DispEventUnadvise((IDispatch*)spButtonSetting);
	IDispEventSimpleImpl<2,CFormAddin,&__uuidof(Office::_CommandBarButtonEvents)>::DispEventUnadvise((IDispatch*)spButtonSend);
	return S_OK;
}

STDMETHODIMP CFormAddin::OnAddInsUpdate(SAFEARRAY * * custom)
{
	return S_OK;
}
STDMETHODIMP CFormAddin::OnStartupComplete(SAFEARRAY * * custom)
{
	return S_OK;
}
STDMETHODIMP CFormAddin::OnBeginShutdown(SAFEARRAY * * custom)
{
	return S_OK;
}

void __stdcall CFormAddin::OnClickSettingButton(IDispatch* Ctrl, VARIANT_BOOL* CancelDefault)
{
	ATLASSERT(spAppCache);
	
	if(!spSettingForm)
		spSettingForm = new CSettingForm();
	spSettingForm->DoModal();


	//Office::_CommandBarButtonPtr cmdButton = Ctrl;
	//
	//_bstr_t caption = cmdButton->Caption;

	//MessageBoxW(NULL,LPCWSTR(caption),L"Hello world!",MB_OK);
}

void __stdcall CFormAddin::OnClickSendButton(IDispatch* Ctrl, VARIANT_BOOL* CancelDefault)
{
	ATLASSERT(spAppCache);
	
	if(!spSendForm)
		spSendForm = new CSendForm(spAppCache);
	spSendForm->DoModal();


}